package cn.sczc.jh.client.service.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczc.jh.client.entity.Permission;
import cn.sczc.jh.client.mapper.sys.PermissionMapper;

@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionService extends ServiceImpl<PermissionMapper, Permission> {

	@Autowired
	private PermissionMapper permissionMapper;

	public List<Permission> getAllPermission() {
		List<Permission> rootPermission = permissionMapper.getPermissionList(-1l);
		if (rootPermission != null && rootPermission.size() > 0) {
			for (Permission p : rootPermission) {
				getChildren(p);
			}
		}
		return rootPermission;
	}

	public void getChildren(Permission p) {
		List<Permission> nodes = permissionMapper.getPermissionList(p.getId());
		if (nodes != null && nodes.size() > 0) {
			for (Permission child : nodes) {
				getChildren(child);
			}
			p.setNodes(nodes);
		}
	}

	public List<Permission> getPermissionByUserId(Long userId) {
		List<Permission> rootPermission = permissionMapper.getPermissionListByUserId(userId, -1l);
		if (rootPermission != null && rootPermission.size() > 0) {
			for (Permission p : rootPermission) {
				getChildrenByUserId(userId, p);
			}
		}
		return rootPermission;
	}

	public void getChildrenByUserId(Long userId, Permission p) {
		List<Permission> nodes = permissionMapper.getPermissionListByUserId(userId, p.getId());
		if (nodes != null && nodes.size() > 0) {
			for (Permission child : nodes) {
				getChildrenByUserId(userId, child);
			}
			p.setNodes(nodes);
		}
	}
}
